/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.model;

import java.io.Serializable;
import java.util.Locale;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;
import org.internna.iwebjtracker.security.Credentials;
import org.internna.iwebmvc.model.Displayable;
import static org.internna.iwebmvc.spring.i18n.WebContextLocale.getActiveLocale;

/**
 * Default mode to connect to a server using a username and a password.
 *
 * @author Jose Noheda
 * @since 1.0
 */
@Embeddable
@DataTransferObject(javascript = "Credentials")
public class UsernamePasswordCredentials implements Credentials, Displayable, Serializable {

    private static final long serialVersionUID = 5541944485694448388L;

    @RemoteProperty
    @Column(name = "USERNAME")
    private String username;

    @RemoteProperty
    @Column(name = "PASSWORD")
    private String password;

    /**
     * Accessor method
     */
    public String getPassword() {
        return password;
    }

    /**
     * Accessor method
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Accessor method
     */
    public String getUsername() {
        return username;
    }

    /**
     * Accessor method
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Warning: This method returns the password in clear text
     */
    @Override public final String getCaption(Locale locale) {
        return "[" + username + "/" + password + "]";
    }

    /**
     * Implements Displayable during DWR conversion.
     */
    @RemoteProperty public final String getDisplayable() {
        return getCaption(getActiveLocale());
    }

}
